package net.wantongtech.xa.uploadbasicinfo.dao;

import com.wtkj.constant.NewClearingConstant;
import com.wtkj.dao.impl.BaseDataTemplate;
import com.wtkj.vo.dzzf.base.qxsjz.entity.MergeTransLog;
import com.wtkj.zwxt.xxjh.share.state.TradeStaticValue;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.util.List;

/**
 * 标记合并交易状态DAO
 *
 * @author MaShibo
 * 日期 :  2020/2/20
 */
@Repository
public class MarkMergeTransStateDao extends BaseDataTemplate {

    public List<MergeTransLog> queryAllErrorMergeLog() {
        final String sql = "SELECT * FROM tr_merge_log tr";
        return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(MergeTransLog.class));
    }

    public Boolean isNoCardTrans(String passId) {
        final String sql =
                "SELECT 1\n" +
                        "  FROM tr_portal_transout tr\n" +
                        "  JOIN tr_dispute_transout ds\n" +
                        "    ON tr.transactionid = ds.transactionid\n" +
                        " WHERE tr.state = ?\n" +
                        "   AND tr.passid = ?\n" +
                        "   AND ds.disputetype = ?";
        return jdbcTemplate.query(sql, ResultSet::next, TradeStaticValue.TradeState.DSPT_ACCOUNT,
                passId, TradeStaticValue.DisputeState.NO_CARDNO);
    }

    public int updateErrorMergeLogState(String passId, int state) {
        final String sql = "UPDATE tr_merge_log tr SET tr.state = ? WHERE tr.passid = ?";
        return jdbcTemplate.update(sql, state, passId);
    }

    public int updateErrorMergeLogState(MergeTransLog mergeTransLog, int state) {
        final String sql = "UPDATE tr_merge_log tr\n" +
                "   SET tr.state = ?\n" +
                " WHERE tr.passid = ?\n" +
                "   AND tr.cardno = ?\n" +
                "   AND tr.tollprovinceid = ?";
        return jdbcTemplate.update(sql, state, mergeTransLog.getPassId(), mergeTransLog.getCardNo(),
                mergeTransLog.getTollProvinceId());
    }


    public List<String> queryTransCardNoByPassId(String passId) {
        final String sql =
                " SELECT tr.etccardid\n" +
                        "  FROM tr_portal_transout tr\n" +
                        " WHERE tr.servicetype = ?\n" +
                        "   AND tr.isintegrated = ?\n" +
                        "   AND tr.state IN (?, ?)\n" +
                        "   AND tr.passid = ?";
        return jdbcTemplate.queryForList(sql, String.class, NewClearingConstant.ServiceType.ETC.getValue(),
                NewClearingConstant.IntegratedState.NONE, TradeStaticValue.TradeState.ACCOUNT,
                TradeStaticValue.TradeState.DSPT_ACCOUNT, passId);
    }
}
